﻿/**
 * @des 检测数字字符串是否为两位，不足两位补齐
 * @param str 要检测的字符串
 */
function getFull(str) {
	if (parseInt(str) == str) {
		if (str < 10) {
			return "0" + str;
		} else {
			return str;
		}
	}
}


/**
 * @des 获取当前的时间串,格式为十四位数字
 * @param str 要转换的时间串
 */
function getNow() {
	var _date = new Date();
	var strTodayTime = "";
	var strY = _date.getFullYear();
	var strM = getFull(_date.getMonth() + 1);
	var strD = getFull(_date.getDate());
	var strH = getFull(_date.getHours());
	var strS = getFull(_date.getMinutes());
	var strMi = getFull(_date.getSeconds());
	
	strTodayTime = strY + strM + strD + strH + strS + strMi;
	
	return strTodayTime;
}


/**
 * @des 获取时间，用于打印服务器端日志用
 */
function logTime() {
	var _date = new Date();
	var strTodayTime = "";
	var strY = _date.getFullYear();
	var strM = getFull(_date.getMonth() + 1);
	var strD = getFull(_date.getDate());
	var strH = getFull(_date.getHours());
	var strS = getFull(_date.getMinutes());
	var strMi = getFull(_date.getSeconds());
	
	strTodayTime = "[" + strY + "-" + strM + "-" + strD + " " + strH + ":" + strS + ":" + strMi + "]";
	
	return strTodayTime;
}


/**
 * @des 显示所有在线用户列表，用于调试
 */
function showAllOnlineMember() {
	var o, names;
	
	names = application.memberList.getPropertyNames();
	
	trace("\n" + logTime() + "\n===========================================\n调试信息（在线人员列表）：")
	for (x in names) {
		o = application.memberList.getProperty(names[x]);
		
		if (o != null) {
			trace("=    系统ID：" + o.systemId + "\n" +
				  "=    成员ID：" + o.memberId + "\n" +
				  "=    成员名称：" + o.memberName + "\n" +
				  "=    登录时间：" + o.loginTime + "\n" +
				  "=    登录IP：" + o.loginIp + "\n" + 
				  "=    登录类型：" + o.loginType == 1 ? "聊天工具" : "会议室");
		}
	}
	trace("===========================================\n");
}


/**
 * @des 显示所有的会议列表，用于调试
 */
function showAllMeeting() {
	var o, names;
	
	names = application.meetingList.getPropertyNames();
	
	trace("\n" + logTime() + "\n===========================================\n调试信息（会议列表）：");
	for (x in names) {
		o = application.meetingList.getProperty(names[x]);
		
		if (o != null) {
			trace("=    会议编号：" + o.id + "\n" +
				  "=    会议模式：" + o.meetingMode + "\n" +
				  "=    会议名称：" + o.meetingName + "\n" +
				  "=    会议主题：" + o.meetingTheme + "\n" +
				  "=    与会人员：" + o.memberList + "\n" +
				  "=    允许私聊：" + o.allowPrivateChat + "\n" +
				  "=    显示所有聊天内容：" + o.showAll + "\n" +
				  "=    创建人：" + o.createUser + "\n" +
				  "=    创建时间：" + o.createTime + "\n");
		}
	}
	trace("===========================================\n");
}

/**
 * @des 顯示所有的通知
 */
function showAllNotify() {
	var o, names;
	
	names = application.notify.getPropertyNames();
	
	trace("\n" + logTime() + "\n===========================================\n调试信息（通知）：");
	for (x in names) {
		o = application.notify.getProperty(names[x]);
		
		if (o != null) {
			trace("=    会议编号：" + o.id + "\n" +
				  "=    会议名称：" + o.meetingName + "\n" +
				  "=    会议主题：" + o.meetingTheme + "\n" +
				  "=    创建人：" + o.createUser + "\n" +
				  "=    创建时间：" + o.createTime + "\n" +
				  "=    被通知人：" + o.memberList + "\n" +
				  "=    备注：" + o.notifyContent + "\n");
		}
	}
	trace("===========================================\n");
}


/**
 * @des 检测会议成员是否已经登录
 * @param client 客户端对象
 * @param memberId 成员Id
 */
function chkMember(client, memberId) {
	var o, names;
	
	names = application.memberList.getPropertyNames();
	
	for(x in names) {
		o = application.memberList.getProperty(names[x]);
		if (o != null) {
			if (o.id == memberId) {
				if (o.ip == client.ip) {
					switch (o.loginType) {
						case 1:
							trace(logTime() + "    " + memberId + "登录聊天工具(Ip:" + o.ip + ")");
							break;
						case 2:
							trace(logTime() + "    " + memberId + "登录视频会议(Ip:" + o.ip + ")");
							break;
						case 3:
							trace(logTime() + "    " + memberId + "打开视频窗口(Ip:" + o.ip + ")");
							break;
					}
				} else {
					trace(logTime() + "    " + memberId + "登录(" + client.ip + ")被拒绝,原因:此用户已在" + o.ip + "登录.");
					var err = new Object();
					err.message = "本地登录被拒绝,您已经在" + o.ip + "上登录!";
					application.rejectConnection(client, err);
					return false;
				}
			}
		}
	}
	return true;
}


/**
 * @des 保存当前客户端信息,将信息存入application.memberList中
 * @param client 要保存的客户端
 * @param memberId 成员Id
 * @param memberName 成员名称
 * @param loginType 连接类型，1 聊天登录　2 会议登录 3 正在与别人私聊．
 *        当loginType 为3时，memberId 为发送人ID与发送人名称，用冒号分隔，memberName 为接收人ID与接收人名称，用冒号分隔
 */
function saveMemberData(client, memberId, memberName, loginType) {
	// 创建一新的成员对象
	var memberObject = new Object();
	memberObject.systemId = application.memberServerId++;		// 系统ID
	memberObject.memberId = memberId;							// 成员ID
	memberObject.memberName = memberName;						// 成员名称
	memberObject.loginTime = getNow();							// 登录时间
	memberObject.loginIp = client.ip;							// 登录Ip
	memberObject.loginType = loginType;							// 登录类型
	memberObject.isChat = false;								// 针对IM，其他登录方式（2,3）省略
	memberObject.client = client;								// 客户端对象
	
	/**
	 * @des 分类存储，会议室登录放入memberList
	 *      IM登录放入IM
	 */
	switch (loginType) {
		case 1:
			trace("\n" + logTime() + "\n===========================================\n" +
				  "=    有新客户端接入(聊天工具)\n" +
				  "=    系统ID：" + memberObject.systemId + "\n" +
				  "=    成员ID：" + memberObject.memberId + "\n" +
				  "=    成员名称：" + memberObject.memberName + "\n" +
				  "=    登录时间：" + memberObject.loginTime + "\n" +
				  "=    登录IP：" + memberObject.loginIp + "\n" +
				  "===========================================\n");
			
			memberObject.im = true;
			application.im.setProperty(memberObject.memberId, memberObject);
			application.memberList.setProperty(memberObject.memeberId, memberObject);
			break;
		case 2:
			trace("\n" + logTime() + "\n===========================================\n" +
				  "=    有新客户端接入(视频会议)\n" +
				  "=    系统ID：" + memberObject.systemId + "\n" +
				  "=    成员ID：" + memberObject.memberId + "\n" +
				  "=    成员名称：" + memberObject.memberName + "\n" +
				  "=    登录时间：" + memberObject.loginTime + "\n" +
				  "=    登录IP：" + memberObject.loginIp + "\n" +
				  "===========================================\n");
			//application.meeting.setProperty(memberObject.memberId, memberObject);
			application.memberList.setProperty(memberObject.memberId, memberObject);
			break;
		case 3:
			trace("\n" + logTime() + "\n===========================================\n" +
				  "=    有新客户端接入(聊天窗口)\n" +
				  "=    系统ID：" + memberObject.systemId + "\n" +
				  "=    发送人：" + memberObject.memberId + "\n" +
				  "=    接收人：" + memberObject.memberName + "\n" +
				  "=    登录时间：" + memberObject.loginTime + "\n" +
				  "=    登录IP：" + memberObject.loginIp + "\n" +
				  "===========================================\n");
			application.chat.setProperty(memberObject.memberId.split(":")[0], memberObject);
			
			// 更新IM列表
			var o = application.im.getProperty(memberId.split(":")[0]);
			if (o != null) {
				o.isChat = true;
				application.im.setProperty(o.memberId, o);
			}
			break;
	}
	
	// 给client对象添加属性
	client.memberId = memberId;
	client.loginType = loginType;
	client.memberName = memberName;
	client.currentMeetingId = "";
}


/**
 * @des 保存正在开会的人员
 * @param meetingId 会议ID
 * @param memberId 成员ID
 * @param membername 成员名称
 */
function saveMeetingMember(meetingId, memberId, memberName) {
	var object = new Object();
	
	object.meetingId = meetingId;
	object.memberId = memberId;
	object.memberName = memberName;
	
	application.meeting.setProperty(object.memberId, object);
}


/**
 * @des 判断用户是否下线
 */
function isOffline(memberId) {
	if (application.im.getProperty(memberId) == null && application.meeting.getProperty(memberId) == null) {
		trace(logTime() + "    " + memberId + " 已离线．．．");
		return true;
	} else {
		return false;
	}
}



/**
 * @des 广播一条消息,可以用来进行客户端的遍历,只广播给指定的客户端,方法是当成员进入一个会场后,进行遍历所有的成员列表,
 *      找出所有与此成员在一个会议室中的其他成员,然后遍历所有的客户端,找出与这些成员Ip相同的客户端进行广播.
 *      广播一条更新音视频流的消息,并且刷新本地的视频调用,以订阅新的视频流.
 * @param cmi 要接受广播的会议室
 * @param cmd 要广播的命令
 * @param para1 当前成员ID,为空则认为是服务器自身发出的广播。
 * @param para2 广播的命令所带的其他参数
 * @param loginType 登錄類型，用於以後的擴展，在這裡進行廣播時只用判斷是否會場等於傳過來的就能唯一確定客戶端．
 */
function broadcastCmd(cmi, cmd, para1, para2, loginType) {
	/**
	 * @des 判断是否为广播至所有连接的客户端,cmi为空则广播所有连接的客户端,否则广播至cmi会场
	 */
	if (cmi == null) {
		application.broadcastMsg(cmd, para2);
	} else {
		// 遍历所有的客户端,广播命令至连接到cmi会场的所有客户端
		for (var i = 0; i < application.clients.length; i++) {
			if (application.clients[i].currentMeetingId == cmi) {
				application.clients[i].call(cmd, null, para1, para2);
			}
		}
	}
}